var myOrder = {
	noCouponTiphtml : '<div class="mod_empty_page"><div class="mod_empty_page_icon"><img src="themes/img/tip/59e46980N2658925e.png"></div> <p class="mod_empty_page_main">您当前无订单</p></div>',
	orderStatus:0
};

//我的订单列表
myOrder.list = function(){
	openLoad();	
	var options = {
		userId:Feng.userId
	}	
	var ajax = new $ax(Feng.ctxPath+"/user/userOrder", function(data){        

        if (data.success){
        	console.log(data);
        	var allOrder = data.obj.allOrder || [],
        		noPay = data.obj.noPay || [],
        		paid = data.obj.paid || [],
        		canceled = data.obj.canceled || [],
        		refunds = data.obj.refunds || [];
        	
        	
        	//时间
        	template.helper('dateFormat', function (inp) {
		        if(inp!==""){
		            return inp.split(' ')[0];
		        }
		    });  
		     //图片过滤器
		    template.helper('pictureFormat', function (inp) {
		       if(inp){
		           return Feng.ctxPath+"/file/getImage/"+inp;
		       }
		       else{
		        	return "themes/img/pd/course_defult.jpg";
		       }
		    });
		    //订单状态过滤器
            template.helper('orderStateFormat', function (inp) {

				if (inp =='1') {
					return '未支付';
				} else if(inp =='2'){
					return '已支付';
				}else if(inp =='3'){
					return '已退款';
				}else if(inp =='4'){
					return '取消订单';
				}else if(inp =='5'){
					return '退款中';
				};
            });
             //价格
            template.helper('orderAmountFormat', function (inp) {
                if(inp!==""){
                    return numeral(inp).format('0,0.00');
                }
            });


		    //全部订单
		    if(allOrder.length > 0){
		    	var outHtml = template("couponTempl",allOrder);
    			$(".tab-panel-item:eq(0) ul").html(outHtml);
		    }
		    else{
		    	$(".tab-panel-item:eq(0)").html(myOrder.noCouponTiphtml);
		    };
		    
////		    //待付款
		    if(noPay.length > 0){
		    	var outHtml = template("couponTempl",noPay);
    			$(".tab-panel-item:eq(1) ul").html(outHtml);
		    }
		    else{
		    	$(".tab-panel-item:eq(1)").html(myOrder.noCouponTiphtml);
		    };
//		    //已付款
		    if(paid.length > 0){
		    	var outHtml = template("couponTempl",paid);
    			$(".tab-panel-item:eq(2) ul").html(outHtml);
		    }
		    else{
		    	$(".tab-panel-item:eq(2)").html(myOrder.noCouponTiphtml);
		    };
		    //退款订单  (退款订单海分两种状态 3 4)
		    if(refunds.length > 0){
		    	var outHtml = template("couponTempl",refunds);
    			$(".tab-panel-item:eq(3) ul").html(outHtml);
		    }
		    else{
		    	$(".tab-panel-item:eq(3)").html(myOrder.noCouponTiphtml);
		    };	    

      	
        }
        else if(data.code === 2){
        	Feng.open(data.msg);
        }
        else{
        	Feng.log("未查询到我的订单");
        }        
        closeLoad();
   },function(data){    	
        Feng.log("我的订单查询失败");
        closeLoad();
    });
    ajax.set(options);
    ajax.start();
};


myOrder.tab = function(){
	var $tab = $('#J_Tab');
	$tab.tab({
	    nav: '.tab-nav-item',
	    panel: '.tab-panel-item',
	    activeClass: 'tab-active'
	});
	
	$tab.find('.tab-nav-item').on('opened.ydui.tab', function (e) {
	    console.log('索引：%s - [%s]已经打开了', e.index, $(this).text());
	});
	
	var args = getQueryStringArgs();
	var orderStatus = args["orderStatus"] || 0;
	
	$(".tab-nav .tab-nav-item").eq(orderStatus).addClass(" tab-active");
	$(".tab-panel .tab-panel-item").eq(orderStatus).addClass(" tab-active");

};

function deleteOrder (OrderNum) {
	var options = {
		orderNum:OrderNum, 
		orderState:4
	};
	
	layer.confirm('是否取消订单', {icon: 10, title:'是否取消订单?'}, function(index){
       //判断课程是否添加
		var ajax = new $ax(Feng.ctxPath+"/order/updateOrderState", function(data){
			if (data.success){
				Feng.success("取消成功");
				myOrder.list();
				
				layer.close(index);
	
	
			} else{
				Feng.error("取消失败");
			}
		},function(data){
			Feng.log("取消失败");
		});
		ajax.set(options);
		ajax.start();
		
    });
	

}


$(function(){
	myOrder.tab();	
	
	
	
	myOrder.list();
});
